﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using CalcSharp.Core.Controlls;

namespace CalcSharp.MDIUI
{
    public partial class HelpDisplay : Form
    {
        private string helpfile;
        private HelpReader hr;

        TreeNode root = new TreeNode("Calc# Help");
        TreeNode tmp;
        TreeNode[] find;

        public HelpDisplay()
        {
            InitializeComponent();
            helpfile = Path.GetDirectoryName(Application.ExecutablePath) + "\\help.zip";
            hr = new HelpReader(helpfile);
        }

        public string[] Documented
        {
            get { return hr.ListCmdHelps(); }
        }

        public static string[] GetDocumentedPlugins()
        {
            string help;
            List<string> documented = new List<string>();
            string[] backup = Program.cmds;
            foreach (string command in Program.loaded.CommandPlugins)
            {
                try
                {
                    Program.cmds = new string[] { command };
                    help = Program.loaded.GetCMDPluginIface(command).Help;
                    if (!string.IsNullOrEmpty(help)) documented.Add(command);
                }
                catch (Exception) { }
            }
            foreach (string command in Program.loaded.WindowPlugins)
            {
                try
                {
                    Program.cmds = new string[] { command };
                    help = Program.loaded.GetWindowPluginIface(command).Help;
                    if (!string.IsNullOrEmpty(help)) documented.Add(command);
                }
                catch (Exception) { }
            }
            Program.cmds = backup;
            help = null;
            return documented.ToArray();
        }

        private void HelpDisplay_Load(object sender, EventArgs e)
        {
            #region Topic tree setup
            TopicTree.Nodes.Clear();
            root.Name = "root";
            string[] contents = hr.ListContents();
            string[] path;
            for (int j = 0; j < contents.Length; j++)
            {
                path = contents[j].Split('/');
                for (int i = 0; i < path.Length; i++)
                {
                    tmp = new TreeNode(path[i].Replace(".rtf",""));
                    tmp.Name = path[i].Replace(".rtf", "");
                    if (i > 0)
                    {
                        find = root.Nodes.Find(path[i - 1], true);
                        if (find.Length > 0) find[0].Nodes.Add(tmp);
                        else root.Nodes.Add(tmp);
                    }
                    else
                    {
                        find = root.Nodes.Find(path[i], false);
                        if (find.Length == 0) root.Nodes.Add(tmp);
                    }
                }
            }
            foreach (TreeNode n in root.Nodes)
            {
                if (n.Nodes.Count < 1) root.Nodes.Remove(n);
            }
            root.Expand();
            TopicTree.Nodes.Add(root);
            #endregion
            LbContents.Items.AddRange(hr.ListTopics());
            TbQsearch.AutoCompleteCustomSource.AddRange(hr.ListTopics());
        }

        public void DisplayCommandHelp(string cmd)
        {
            if (hr.CommmandHelpExists(cmd))
            {
                this.Show();
                this.ContentDisplay.Rtf = hr.GetHelpContent("cmd/" + cmd + ".rtf");
                this.LbContents.SelectedIndex = LbContents.Items.IndexOf(cmd);
            }
        }

        private void TopicTree_DoubleClick(object sender, EventArgs e)
        {
            if (TopicTree.SelectedNode != null)
            {
                if (TopicTree.SelectedNode.Nodes.Count == 0)
                {
                    string path = TopicTree.SelectedNode.FullPath;
                    path = path.Replace(root.Text+"\\", "");
                    path = path.Replace("\\", "/");
                    path += ".rtf";
                    this.ContentDisplay.Rtf = hr.GetHelpContent(path);
                    ChangeTitle(TopicTree.SelectedNode.Text);
                }
            }
        }

        private void LbContents_DoubleClick(object sender, EventArgs e)
        {
            if (LbContents.SelectedItem != null)
            {
                ChangeTitle(LbContents.SelectedItem.ToString());
                string path = hr.GetHelpContentPath(LbContents.SelectedItem.ToString());
                this.ContentDisplay.Rtf = hr.GetHelpContent(path);
            }
        }

        private void TbQsearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && !string.IsNullOrEmpty(TbQsearch.Text))
            {
                this.LbContents.SelectedIndex = LbContents.Items.IndexOf(TbQsearch.Text);
                if (this.LbContents.SelectedIndex != -1)
                {
                    string path = hr.GetHelpContentPath(LbContents.SelectedItem.ToString());
                    this.ContentDisplay.Rtf = hr.GetHelpContent(path);
                    ChangeTitle(TbQsearch.Text);
                }
            }
        }

        private void ChangeTitle(string contenttitle)
        {
            if (!string.IsNullOrEmpty(contenttitle)) this.Text = "Help Browser - " + contenttitle;
            else this.Text = "Help Browser";
        }
    }
}
